/**
 * 详情对话框
 */
var CouponInfoDlg = {
    data: {
        couponName: "",
        isOpen: "",
        discountType: "",
        sendNumber: "",
        satistyMoney: "",
        discountMoney: "",
        discount: "",
        startTime: "",
        endTime: "",
        couponDesc: "",
        createTime: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/coupon/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    if(result.data.discountType==1){
        $("#discountM").show();
        $("#discountZ").hide();
    }else if(result.data.discountType==2){
        $("#discountM").hide();
        $("#discountZ").show();
    }
    form.val('couponForm', result.data);

    form.on('select(discountType)',function (data) {
        if (data.value == 1) {
            $("#discountM").show();
            $("#discountZ").hide();
        } else if (data.value == 2) {
            $("#discountM").hide();
            $("#discountZ").show();
        }
    });
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/coupon/editItem", function (data) {
            Feng.success("更新成功！");
            window.location.href = Feng.ctxPath + '/coupon'
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/coupon'
    });
    var startDate = laydate.render({
        elem: '#laydate-startTime',
        max: $('#laydate-endTime').val(),
        theme: 'molv',
        trigger: 'click',
        done: function (value, date) {
            // 结束时间大于开始时间
            if (value !== '') {
                endDate.config.min.year = date.year;
                endDate.config.min.month = date.month - 1;
                endDate.config.min.date = date.date;
            } else {
                endDate.config.min.year = '';
                endDate.config.min.month = '';
                endDate.config.min.date = '';
            }
        }
    });

    var endDate = laydate.render({
        elem: '#laydate-endTime',
        min: $('#laydate-startTime').val(),
        theme: 'molv',
        trigger: 'click',
        done: function (value, date) {
            // 开始时间小于结束时间
            if (value !== '') {
                startDate.config.max.year = date.year;
                startDate.config.max.month = date.month - 1;
                startDate.config.max.date = date.date;
            } else {
                startDate.config.max.year = '';
                startDate.config.max.month = '';
                startDate.config.max.date = '';
            }
        }
    });

});